package com.imooc.config;

import com.imooc.base.BaseInfoProperties;
import com.imooc.exception.GraceException;
import com.imooc.pojo.SysParams;
import com.imooc.result.ResponseStatusEnum;
import com.imooc.service.SysParamsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.Objects;

/**
 * @author liansen
 * @create 05-31-15:06
 */


/**
 * 实现CommandLineRunner的目的是要在resource服务启动之后, 直接运行run方法, 以达到缓存预热的目的
 */
@Configuration
public class SysParamConfig extends BaseInfoProperties implements CommandLineRunner {

    @Autowired
    private SysParamsService sysParamsService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public void run(String... args) throws Exception {
        SysParams sysParams = sysParamsService.getById(1001);
        if(Objects.isNull(sysParams)){
            GraceException.display(ResponseStatusEnum.SYSTEM_PARAMS_SETTINGS_ERROR);
        }
        dealMaxResumeRefreshCounts(sysParams.getMaxResumeRefreshCounts());
    }

    /**
     * 对简历每日刷新的最大次数做缓存预热处理
     */
    public void dealMaxResumeRefreshCounts(int counts){
        redisTemplate.opsForValue().set(REDIS_MAX_RESUME_REFRESH_COUNTS, counts + "");
    }
}
